package com.aipp.mall.common.utils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author ruoyi
 */
public class DateUtils {

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static String parseDateToStr(final String format, Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 获取2个时间间隔天数
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    public static Long betweenDay(LocalDateTime beginTime, LocalDateTime endTime) {
        return ChronoUnit.DAYS.between(beginTime.toLocalDate().atStartOfDay(), endTime.toLocalDate().atStartOfDay());
    }

}
